package net.orange.architecture.audit;

import net.orange.architecture.system.SystemParameters;

import org.apache.log4j.DailyRollingFileAppender;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;

/**
 * The security auditor - track down all the user's authority violation in log
 * file.
 */
public class SecurityFileAuditor implements IAuditor {
	
	/**
	 * The file log4j object.
	 */
	private Logger logger = null;
	
	/**
	 * The file layout format
	 */
	private PatternLayout layout = null;
	
	/**
	 * The daily rolling file appender
	 */
	private DailyRollingFileAppender appender = null;
	
	/**
	 * The default constructor to initialize the object.
	 */
	public SecurityFileAuditor() {
		logger = Logger.getLogger(SecurityFileAuditor.class);
		logger.removeAllAppenders();

		layout = new PatternLayout();
		layout.setConversionPattern("%d{ABSOLUTE} %-5p [%c{1}] %m%n");
		try {
			appender = new DailyRollingFileAppender(layout,
					SystemParameters.AD_SECURITY_DIR, "'.'yyyy-MM-dd");
			logger.addAppender(appender);
		} catch (Exception e) {
			System.out.println("[Fatal Error] Can't initialize the log file:"
					+ SystemParameters.AD_SECURITY_DIR + "\n");
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.orange.architecture.audit.IAuditor#update(net.orange.architecture.audit.IAuditInfo)
	 */
	public void update(IAuditInfo info) {
		if (SystemParameters.ARCH_LOG_SECRITY)
			logger.info(info.toInfo());
	}
}
